package org.example.day11;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 4.1.12.2 lock.isHeldByCurrentThread()  判断当前线程是否持有此锁
 **/
public class MyThread13 {
    private ReentrantLock lock;
    public MyThread13(boolean isFair){
        lock = new ReentrantLock(isFair);
    }
    public void serviceMethod(){
        try {
            System.out.println(this); //org.example.day11.MyThread13@48369750
            System.out.println(Thread.currentThread().getName()); //Thread-0
            System.out.println(lock.isHeldByCurrentThread()); //false
            lock.lock();
            System.out.println(lock.isHeldByCurrentThread());  //true
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        final MyThread13 m13 = new MyThread13(true);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                m13.serviceMethod();
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
}
